/*
 * Copyright @ 2024 weili23
 * com.sky.task 17:59
 * All right reserved.
 */

package com.sky.task;

import com.sky.entity.Orders;
import com.sky.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @desc:
 * @author: weili23
 * @createTime: 2024/11/11 17:59
 * @version: v1.0
 */
@Component
@Slf4j
public class OrderTask {
    @Resource
    private OrderService orderService;

    /**
     * @desc: 处理用户超时支付的订单超时任务
     * @author: weili23
     * @Date: 2024/11/11 18:00
     * @version: 1.0
     */
    @Scheduled(cron = "0 * * * * ?")
    public void processOrderTimeOutPayTL(){
        log.info("====开始处理订单超时任务====");
        //查询订单超过15分钟还没支付的
        List<Orders> list = orderService.processOrderTimeOutTask(Orders.PENDING_PAYMENT, LocalDateTime.now().plusMinutes(-15));
        //2.有超时未处理的订单时
        for (Orders orders : list) {
            orders.setStatus(Orders.CANCELLED);
            orders.setCancelReason("超时未支付，自动取消");
            orders.setCancelTime(LocalDateTime.now());
            orderService.updateById(orders);
        }

    }

    /**
     * @desc: 处理订单在派送中的订单，每天凌晨一点自动执行
     * @author: weili23
     * @Date: 2024/11/12 11:12
     * @version: 1.0
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void processOrderTimeOutDeliverTL(){
        log.info("====开始处理长时间派送中任务====");
        //订单在派送中的订单，每天凌晨一点自动执行
        List<Orders> list = orderService.processOrderTimeOutTask(Orders.DELIVERY_IN_PROGRESS, LocalDateTime.now().plusMinutes(-60));
        //2.有超时未处理的订单时
        for (Orders orders : list) {
            orders.setStatus(Orders.COMPLETED);
            orderService.updateById(orders);
        }
    }
}
